// pages/shop_mann/shop_mann.js
const get_from = require('../../module/service.js').get_from;
const post_to = require('../../module/service.js').post_to;
const isChanged = require('../../module/isChanged.js').isChanged;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopInfo: {
      shop_id: 0,
      name: '',
      banner_urls: '',
      description: '',
      icon_url: ''
    },
    infoBak: {
      shop_id: 0,
      name: '',
      banner_urls: '',
      description: '',
      icon_url: ''
    },
    show_shopInfo: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let shop_id = parseInt(options.shop_id);
    if (shop_id)
      this.setData({
        'shopInfo.shop_id': shop_id,
        'infoBak.shop_id': shop_id
      });

    //请求后端获取商店基础信息
    let shopInfo = {
      shop_id: shop_id,
      name: '',
      banner_urls: '',
      description: '',
      icon_url: ''
    };

    get_from('shop/getInfoByShopId', {shop_id: shop_id}).then((res)=>{
      shopInfo.name = res.data.name;
      shopInfo.description = res.data.description;
      shopInfo.icon_url = res.data.icon_url
      for (let i=0; i<res.data.banner_urls.length; i++)
      {
        shopInfo.banner_urls += res.data.banner_urls[i].img_url+';';
      }
      console.log(res.data);
      this.setData({
        shopInfo: shopInfo
        // infoBak: shopInfo
      });
      //数据备份
      // this.data.infoBak = shopInfo;  //这种方式是以地址传递的方式复制的
      this.data.infoBak = {...shopInfo};
      console.log(this.data.infoBak);
    }, ()=>{
      this.setData({
        shopInfo: shopInfo
      });
    });
  },

  show_shopInfo(){
    this.setData({
      show_shopInfo: !this.data.show_shopInfo
    });
  },

  to_shop_menu_mann(){
    //跳转到菜品管理页面
    wx.navigateTo({
      url: '/pages/food_mann/food_mann?shop_id='+this.data.shopInfo.shop_id,
    })
  },

  to_shop_food_cate(){

  },

  ed_shop_name(e){
    this.setData({
      'shopInfo.name': e.detail.value
    });
    // console.log(this.data.shopInfo.name);
  },

  ed_shop_bann(e){
    this.setData({
      'shopInfo.banner_urls': e.detail.value
    });
    console.log(this.data.infoBak.banner_urls);
  },

  ed_shop_desc(e){
    this.setData({
      'shopInfo.description': e.detail.value
    });
  },
  
  ed_shop_icon(e){
    this.setData({
      'shopInfo.icon_url': e.detail.value
    });
  },

  reset(){
    this.setData({
      shopInfo: {...this.data.infoBak}
    });
  },

  ensure(){
    //提交数据给后端
    let shopInfo = {
      shop_id: this.data.shopInfo.shop_id,
      name: '',
      banner_urls: '',
      description: '',
      icon_url: ''
    };
    //判断哪个改了哪个没改
    if (this.data.shopInfo.name !== this.data.infoBak.name)
    {
      shopInfo.name = this.data.shopInfo.name;
    }
    if (this.data.shopInfo.banner_urls !== this.data.infoBak.banner_urls)
    {
      shopInfo.banner_urls = this.data.shopInfo.banner_urls;
    }
    if (this.data.shopInfo.description !== this.data.infoBak.description)
    {
      shopInfo.description = this.data.shopInfo.description;
    }
    if (this.data.shopInfo.icon_url !== this.data.infoBak.icon_url)
    {
      shopInfo.icon_url = this.data.shopInfo.icon_url;
    }

    post_to('shop/modi_shopInfo', shopInfo).then((res)=>{
      if (res.data.msg != 'success')
      {
        console.log('修改失败，错误：', res.data.msg);
        return;
      }

      this.setData({
        show_shopInfo: !this.data.show_shopInfo
      });
      wx.showToast({
        title: '修改成功!',
      });
    }, ()=>{
      console.log('修改失败');
    });
  },

  cancel(){
    this.setData({
      show_shopInfo: !this.data.show_shopInfo
    });
  }
})